var express = require('express'),
    bodyParser = require('body-parser'),
    
    cookieParser = require('cookie-parser'),
    multer = require('multer'),
    fs = require('fs'),
    template = require('art-template')
//实例化对象
var app = express(),
    form = multer()

//设置文件存储
var storage = multer.diskStorage({
        destination: 'www/uploads',
        filename: function(req, file, callback){
            var petname = req.cookies.petname
            callback(null, petname + '.jpg')
        }
    }),
    uploads = multer({storage})

//设置中间件
app.engine('.html', template.__express)
app.set('view engine', 'html')
// 关闭本地缓存
template.config('cache', false)


app.use(express.static('www'))
app.use(bodyParser.urlencoded({extended:false}))
app.use(cookieParser())
/*-------------------- 注册  --------------------*/
app.use(require('./routers/zhuce'))
/*----------------  登录请求  -------------------*/
app.use(require('./routers/login'))
/*---------------- 退出请求 ---------------------------*/
app.use(require('./routers/tuichu'))
/*--------------------文件上传处理 --------------------*/
app.post('/user/photo',uploads.single('photo'), function(req, res){
    res.status(200).json({code:'success', message:'上传成功,'})
})

/*------------------------提问请求---------------------*/
 app.use(require('./routers/ask'))
/*----------------- 首页获取提问的内容信息 ---------------*/
 app.use(require('./routers/question'))
//----------------回答数据处理------------------
 app.use(require('./routers/answer'))



// 首页处理
app.get('/', function (req, res) {
    fs.exists('questions', function (ex) {
        if(!ex){
            res.render('template',{
                title:'网站首页'
            })
        }else{
            fs.readdir('questions',function(err,files){
                if(err){
                    res.render('template',{
                title:'网站首页'
                    })
                }else{
                    console.log(files)
                var files = files.reverse()
                var ques = []
                for(var i = 0;i< files.length ;i++) {
                  var filePath = 'questions/' + files[i]
                  //同步读文件
                  var data = fs.readFileSync(filePath)
                  data = JSON.parse(data)
                  ques.push(data)
                  
                    
                   
                }   
                console.log(ques)
                res.render('template',{
                    title:'网站首页',datas:ques
                })
            }
            })
        }
    })
})
  
  
app.get('/answer.html', function(req,res){
    console.log('现在开始渲染提问页面了')
    res.render('answer',{
        title:'提问页面',
        name:'answer'
    })
})

app.get('/login.html', function(req,res){
    console.log('现在开始渲染登录页面了')
    res.render('login',{
        title:'登录页面',
        name:'login'
    })
})

app.get('/ask.html', function(req,res){
    res.render('ask',{
        title:'回答页面',
        name:'ask'
    })
})
app.get('/register.html', function(req,res){
    res.render('register',{
        title:'注册页面',
        name:'register'
    })
})
app.get('/user.html', function(req,res){
    res.render('user',{
        title:'个人资料',
        name:'user'
    })
})
 app.get('/template.html', function(req,res){
    res.render('template',{
        title:'首页',
        name:'template'
    })
})    
  /*-----------辅助方法   转化为 年 月 日 时 分 秒------- */
  
  template.helper('dateFormate',function(asks){
    var time = new Date(asks)
    var year = time.getFullYear()
    var month = time.getMonth()
    var date = time.getDate()
    var hour = time.getHours()
    var minutes = time.getMinutes()
    var second = time.getSeconds()
    hour = hour < 10 ? '0' + hour : hour
    minutes = minutes < 10 ? '0' + minutes :minutes
    second = second < 10 ? '0' + second : second
    var str = year + '-' + month +'-' +date + ' ' + hour+ ':' + minutes + ':' + second
    
    return str 
})


template.helper('fileName', function(date) {
    var time = new Date(date).getTime()
    return time
})

// ----------端口号 -----------------
app.listen(3000, function(){
    console.log( 'success' )
})
